<?php
namespace app\api\controller;

use \think\Controller;
use \think\Db;
require_once ('../vendor/qiniu/autoload.php');	
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
class Common extends Controller
{
    public function dua22nxin(){
        $sid = 'c22623fbab15f3fd208c123f772116f4';
        $token = '44b2970319a051f56c7309dcfe2ebef1';
        $appid = 'a199f95f61f74e3abace3cff3cce7773';
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, "https://open.ucpaas.com/ol/sms/sendsms?sid={$sid}&appid={$appid}&mobile=15895980214&token={$token}&templateid=271254");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        var_dump($output);
    }
    public function duaxin($phone,$num){
        $temid = '296118';
        if($num == 1){//给选定领养人发送短信
            $num = '';
            $temid = '296124';
        }
        if($num == 2){//给提交实名的人的提醒
            $num = '';
            $temid = '300807';
        }
        $body_json = array(
            'sid'=>'c22623fbab15f3fd208c123f772116f4',
            'token'=>'44b2970319a051f56c7309dcfe2ebef1',
            'appid'=>'cd8b5f4a3db0494cb72e299809c36f20',
            'templateid'=>$temid,
            'param'=>$num,
            'mobile'=>$phone,
            'uid'=>'',
        );
        $body = json_encode($body_json);
        $header = array(
            'Accept:application/json',
            'Content-Type:application/json;charset=utf-8',
        );
        $url = 'https://open.ucpaas.com/ol/sms/sendsms';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($result,true);
        return $res;
    }

    function duanxin(){
//        require_once('../vendor/duanxin/lib/Ucpaas.class.php');
//        require_once('../vendor/duanxin/serverSid.php');
//        $appid = "a199f95f61f74e3abace3cff3cce7773";	//应用的ID，可在开发者控制台内的短信产品下查看
//        $templateid = "271253";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
//        $param = $num; //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
//        $mobile = $phone;
//        $uid = "";
//
////70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
//
//        $res = $ucpass->SendSms($appid,$templateid,$param,$mobile,$uid);
//        return json_encode(['status'=>22,'msg'=>'已经有此手机号码用户']);
////        $res = json_decode($res,true);
////        return $res;
////        if($res['code'] != '00000'){
////            return json_encode(['status'=>3,'msg'=>$res['msg']]);
////        }
    }
public function gettoken(){//echo 1;die;
header("Access-Control-Allow-Origin:*");
    $accessKey = 'vN-QggRwoyeo6yBxxOPBamVG2ja2V2ANfzp-8v5k';
    $secretKey = 'NFWueX9frwxTQquf40Q2j7yL3VUD2BhJ6d37JiCR';
    $bucket = 'yidog';
    $auth = new Auth($accessKey, $secretKey);
    
    $token = $auth->uploadToken($bucket);
	
    return json_encode(['msg'=>$token]);
    }
public function rongyun($uid,$name,$imageurl){//echo 1;die;
    $appKey = '3argexb63mahe';
    $appSecret = 'ZwgetAWvUKlF';
    $body = "userId={$uid}&name={$name}&portraitUri={$imageurl}";
    $nonce = mt_rand();
    $timeStamp = time();
    $sign = sha1($appSecret.$nonce.$timeStamp);
    $header = array(
        'App-Key:'.$appKey,
        'Nonce:'.$nonce,
        'Timestamp:'.$timeStamp,
        'Signature:'.$sign,
        'Content-Type: application/x-www-form-urlencoded'
    );
    $url = 'http://api.cn.ronghub.com/user/getToken.json';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
//    curl_setopt($ch, CURLOPT_SSL_VERI/FYPEER, false);
//    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $result = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($result,true);
    return $res;
//    var_dump($res) ;
    }

    /**
     * 验证唯一
     * user_id，time，salt，value
     * @return string
     */
//  var reqDate = Date.now();
//  var appId = "A6060432236270";
//  var appkey = "F591B37E-1C61-01C7-57C6-8B2F6D59DCD9";
//  var reqKey = MD5(appId + "XW" + appkey + "XW" + reqDate);
//  var Token = MD5(userId + "XW" + token + "XW" + reqDate);
    public  function checkAll($uid,$time,$value,$reqKey){//$uid,$time,$value
        if($uid == 0){
            return true;
        }else{
            $user = Db::table('up_u_user')->where('user_id',$uid)->find();
            $token = $user['user_token'];
            $appId = "A6060432236270";
            $appkey = "F591B37E-1C61-01C7-57C6-8B2F6D59DCD9";
            $myreqKey = md5($appId . 'XW' . $appkey . 'XW' . $time);
            $myvalue = md5($uid . 'XW' . $token . 'XW' . $time);
            if($myvalue != $value || $myreqKey != $reqKey){
                return false;
            }else{
                return true;
            }
        }
    }



    /**
     * getHomePageData 获取首页数据
     * @return 返回数组
     */
    public function getHomePageData($where,$page){
        if($page>=10){
            $page = 10;
        }
        $where1 = $where;
        $where2 = array();
        $where3 = array();
        $where4 = array();
        $where1['dog_status'] = 1;
        $where2['dog_help_status'] = 1;
        $where3['dog_seek_status'] = 1;
        $where4['dog_adopt_status'] = array('in','2,4');
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area u','ud.area_sheng=u.district_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->join('up_dog_seek_characteristic uds','ud.dog_seek_characteristic=uds.udskc_id','left')
            ->where($where)
            ->where (function ($query){
                $query->where( 'ud.dog_status',1)->whereOr( 'ud.dog_help_status',1)->whereOr('ud.dog_seek_status',1)->whereOr('ud.dog_adopt_status','IN',[2,4]);
            })
            ->field('ud.dog_type,ud.dog_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,
ua.district shi,uaa.district qu,ud.dog_num,ud.udsc_id_information,ud.dog_status,ud.dog_img,ud.udsc_id,ud.dog_release_time,ud.dog_add,udf.udf_name,ud.dog_help_status,ud.dog_information,ud.dog_end_time,ud.dog_help_information,ud.dog_help_style,u.district shen,ud.dog_adopt_status,udf.udf_sex,udf.udf_age,udf.udf_dog_sterilization,udf.udf_dog_outside,udf.udf_dog_inside,udf.udf_dog_vaccine,udf.udf_img,uuu.user_username,uuu.user_image,uuu.user_attestation,ud.dog_seek_time,ud.dog_seek_status,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskci_id_other')
            ->order('ud.dog_release_time desc')
            ->limit(($page-1)*10,10)
            ->select();
        for($i = 0;$i<count($data);$i++){
            if($data[$i]['dog_type'] == 1){
                $a = trim($data[$i]['udsc_id'],',');
                $b = Db::table('up_dog_strat_characteristic')
                    ->where('udsc_id','IN',$a)
                    ->column('udsc_name');
                $num = explode(',',$a);
                if(in_array(0,$num)){
                    $b['other'] = $data[$i]['udsc_id_information'];
                }
                $data[$i]['characteristic'] = $b;
            }elseif($data[$i]['dog_type'] == 2){
                $a = trim($data[$i]['dog_help_style'],',');
                $b = Db::table('up_dog_heo')
                    ->where('heo_id','IN',$a)
                    ->column('heo_name');
                $num = explode(',',$a);
                if(in_array(0,$num)){
                    $b['other'] = $data[$i]['dog_help_information'];
                }
                $data[$i]['heo_category'] = $b;
            }elseif($data[$i]['dog_type'] == 4){
                $b = Db::table('up_dog_seek_characteristic_apparel')
                    ->where('udskca_id','IN',$data[$i]['udskca_id'])
                    ->column('udskca_name');
                $a = Db::table('up_dog_seek_characteristic_other')
                    ->where('udsci_id','IN',$data[$i]['udskci_id'])
                    ->column('udsci_name');
                $b_num = explode(',',$data[$i]['udskca_id']);
                $a_num = explode(',',$data[$i]['udskci_id']);
                if(in_array(0,$b_num)){
                    $b['other'] = $data[$i]['udskca_id_other'];
                }
                if(in_array(0,$a_num)){
                    $a['other'] = $data[$i]['udskci_id_other'];
                }

                $data[$i]['apparel'] = $b;
                $data[$i]['others'] = $a;
            }
        }
        return $data;
    }



    /**
     * @param $client
     * @param $sence
     * @param $orderId
     * @return array|bool
     * 极光推送
     */
    public function Jpush($token,$msg,$msg_data){
        //require_once 'vendor/jpush/jpush/autoload.php';
        $ji=array(
            'appKey'=>'9ebabf09dc59c31d83129881',
            'masterSecret'=>'335e7c9ef4e81f2be4b747af',
        );
        $client = new \JPush\Client($ji['appKey'],$ji['masterSecret']);
        $push = $client->push();
        $push->setPlatform('all');
            $push->addAlias($token);

        $push->iosNotification($msg, array(
                'alert'=>$msg,
                'badge' => '1',
                'extras' => $msg_data,
                'sound'=>'default'
            )
        );
        $push->androidNotification($msg, array(
            'badge' => '1',
            'extras' => $msg_data
        ));
        return $push->send();

    }
}